/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.Message;
import com.hboxs.asl.Order;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.entity.Category;
import com.hboxs.asl.entity.Essay;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.ProductImage;
import com.hboxs.asl.service.CategoryService;
import com.hboxs.asl.service.EssayService;
import com.hboxs.asl.service.MemberService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpSession;

import javax.annotation.Resource;
import java.util.List;

/**
 * Controller - 文章
 */
@Controller("adminEssayController")
@RequestMapping("/asl-admin/essay")
public class EssayController extends BaseController {

    @Resource(name = "essayServiceImpl")
    private EssayService essayService;
    @Resource(name = "memberServiceImpl")
    private MemberService memberService;
    @Resource(name = "categoryServiceImpl")
    private CategoryService categoryService;

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model, HttpSession session) {
        List<Member> members = memberService.findAll();
        model.addAttribute("members", members);
        List<Category> categories = categoryService.findAll();
        model.addAttribute("categories", categories);
        session.setAttribute(IMAGE_TYPE, ProductImage.imageType.essay);
        return "/admin/essay/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Essay essay, Long memberId, Long categoryId, RedirectAttributes redirectAttributes) {
        try {
            if(essay == null) {
                return ERROR_VIEW;
            }
            Member member = memberService.find(memberId);
            essay.setMember(member);
            Category category = categoryService.find(categoryId);
            if(category != null) {
                essay.setCategory(category);
            }
            essayService.save(essay);
        } catch (Exception e) {
            e.printStackTrace();
        }
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model, HttpSession session) {
        model.addAttribute("essay", essayService.find(id));
        model.addAttribute("categorys" , categoryService.findAll());
        session.setAttribute(IMAGE_TYPE, ProductImage.imageType.essay);
        return "/admin/essay/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Essay essay, Long categoryId, RedirectAttributes redirectAttributes) {
        if (!isValid(essay)) {
            return ERROR_VIEW;
        }
        Category category = categoryService.find(categoryId);
        if(category != null) {
            essay.setCategory(category);
        }
        essayService.update(essay, "member");
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, ModelMap model) {
        pageable.getOrders().add(Order.desc("createDate"));
        model.addAttribute("page", essayService.findPage(pageable));
        return "/admin/essay/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Message delete(Long[] ids) {
        essayService.delete(ids);
        return SUCCESS_MESSAGE;
    }

}
